﻿using Eqwww.App.Identity;
using Eqwww.Data;
using Eqwww.Domain;
using Eqwww.Domain.Models.Identity;
using Eqwww.Domain.Models.SystemManage;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.DrawingCore;
using System.Linq;
using System.Threading.Tasks;

namespace Eqwww.Web
{
    public class SeedDb
    {
        private UserApp _userApp;
        private RoleApp _roleApp;
        private ApplicationDbContext _context;
        private IModelHandler _modelHandler;

        public SeedDb(UserApp userApp,
            RoleApp roleApp,
            ApplicationDbContext context,
            IModelHandler modelHandler)
        {
            _userApp = userApp;
            _roleApp = roleApp;
            _context = context;
            _modelHandler = modelHandler;
        }

        public void Init()
        {
            if (!_roleApp.Roles.Any())
            {
                var role = new RoleModel
                {
                    Name = "Founder",
                    ShowName = "创始人",
                    IsSystem = true
                };
                _modelHandler.Create(role);
                var result = _roleApp.CreateAsync(role).Result;
            }

            if (!_userApp.Users.Any())
            {
                var user = new UserModel
                {
                    UserName = "waemz@live.com",
                    Email = "waemz@live.com",
                    RealName = "创始人",
                    Avatar = "/images/avatars/default.png",
                    IsSystem = true
                };
                _modelHandler.Create(user);
                var result = _userApp.CreateAsync(user, "1083gxW@");
            }

            var roleFounder = _roleApp.FindByNameAsync("Founder").Result;
            var userFounder = _userApp.FindByEmailAsync("waemz@live.com").Result;

            if (!_userApp.IsInRoleAsync(userFounder, roleFounder.Name).Result)
            {
                var result = _userApp.AddToRoleAsync(userFounder, roleFounder.Name).Result;
            }

            if (!_context.Set<ModuleModel>().Any())
            {
                var modules = new ModuleModel[]
                {
                    new ModuleModel
                    {
                        Code="OrganizeManage",
                        Name="组织管理",
                        SortCode=8800,
                        Icon="language",
                        MenuMark=true,
                        ModuleOpenType=ModuleOpenType.NoAction,
                        IsSystem=true,
                        Layer=0
                    },
                    new ModuleModel
                    {
                        Code="SystemManage",
                        Name="系统管理",
                        SortCode=9000,
                        Icon="settings",
                        MenuMark=true,
                        ModuleOpenType=ModuleOpenType.NoAction,
                        IsSystem=true,
                        Layer=0
                    },
                    new ModuleModel
                    {
                        Code="SystemSecurity",
                        Name="系统维护",
                        SortCode=9500,
                        Icon="settings_backup_restore",
                        MenuMark=true,
                        ModuleOpenType=ModuleOpenType.NoAction,
                        IsSystem=true,
                        Layer=0
                    }

                };
                foreach (var item in modules)
                {
                    _modelHandler.Create(item);
                    _context.Add(item);
                }
                _context.SaveChanges();

                modules = new ModuleModel[]
                {
                    new ModuleModel
                    {
                        Code="SystemManage_ConfigManage",
                        Name="系统设置",
                        SortCode=50,
                        Icon="adb",
                        MenuMark=true,
                        ModuleOpenType=ModuleOpenType.Tab,
                        IsSystem=true,
                        ParentId=_context.Sys_Modules.First(x=>x.Code=="SystemManage").Id,
                        Url="/SystemManage/ConfigManage",
                        Layer=1
                    },

                    new ModuleModel
                    {
                        Code="SystemManage_Module",
                        Name="模块管理",
                        SortCode=100,
                        Icon="list",
                        MenuMark=true,
                        ModuleOpenType=ModuleOpenType.Tab,
                        IsSystem=true,
                        ParentId=_context.Sys_Modules.First(x=>x.Code=="SystemManage").Id,
                        Url="/SystemManage/Module",
                        Layer=1
                    },
                    new ModuleModel
                    {
                        Code="SystemManage_Strategory",
                        Name="策略权限",
                        SortCode=150,
                        Icon="list",
                        MenuMark=true,
                        ModuleOpenType=ModuleOpenType.Tab,
                        IsSystem=true,
                        ParentId=_context.Sys_Modules.First(x=>x.Code=="SystemManage").Id,
                        Url="/SystemManage/Strategory",
                        Layer=1
                    },
                    new ModuleModel
                    {
                        Code="SystemManage_Role",
                        Name="角色管理",
                        SortCode=200,
                        Icon="list",
                        MenuMark=true,
                        ModuleOpenType=ModuleOpenType.Tab,
                        IsSystem=true,
                        ParentId=_context.Sys_Modules.First(x=>x.Code=="SystemManage").Id,
                        Url="/SystemManage/Role",
                        Layer=1
                    },
                    new ModuleModel
                    {
                        Code="SystemManage_Attachment",
                        Name="附件管理",
                        SortCode=900,
                        Icon="list",
                        MenuMark=true,
                        ModuleOpenType=ModuleOpenType.Tab,
                        IsSystem=true,
                        ParentId=_context.Sys_Modules.First(x=>x.Code=="SystemManage").Id,
                        Url="/SystemManage/Attachment",
                        Layer=1
                    },

                    new ModuleModel
                    {
                        Code="SystemManage_Organize",
                        Name="组织架构",
                        SortCode=300,
                        Icon="list",
                        MenuMark=true,
                        ModuleOpenType=ModuleOpenType.Tab,
                        IsSystem=true,
                        ParentId=_context.Sys_Modules.First(x=>x.Code=="OrganizeManage").Id,
                        Url="/SystemManage/Organize",
                        Layer=1
                    },
                    new ModuleModel
                    {
                        Code="SystemManage_Job",
                        Name="工种管理",
                        SortCode=400,
                        Icon="list",
                        MenuMark=true,
                        ModuleOpenType=ModuleOpenType.Tab,
                        IsSystem=true,
                        ParentId=_context.Sys_Modules.First(x=>x.Code=="OrganizeManage").Id,
                        Url="/SystemManage/Job",
                        Layer=1
                    },
                    new ModuleModel
                    {
                        Code="SystemManage_Post",
                        Name="岗位管理",
                        SortCode=500,
                        Icon="list",
                        MenuMark=true,
                        ModuleOpenType=ModuleOpenType.Tab,
                        IsSystem=true,
                        ParentId=_context.Sys_Modules.First(x=>x.Code=="OrganizeManage").Id,
                        Url="/SystemManage/Post",
                        Layer=1
                    },
                    new ModuleModel
                    {
                        Code="SystemManage_User",
                        Name="用户管理",
                        SortCode=600,
                        Icon="list",
                        MenuMark=true,
                        ModuleOpenType=ModuleOpenType.Tab,
                        IsSystem=true,
                        ParentId=_context.Sys_Modules.First(x=>x.Code=="OrganizeManage").Id,
                        Url="/SystemManage/User",
                        Layer=1
                    },
                    new ModuleModel
                    {
                        Code="SystemSecurity_DatabaseLog",
                        Name="系统日志",
                        SortCode=9900,
                        Icon="list",
                        MenuMark=true,
                        ModuleOpenType=ModuleOpenType.Tab,
                        IsSystem=true,
                        ParentId=_context.Sys_Modules.First(x=>x.Code=="SystemSecurity").Id,
                        Url="/SystemSecurity/DatabaseLog",
                        Layer=1
                    }

                };
                foreach (var item in modules)
                {
                    _modelHandler.Create(item);
                    _context.Add(item);
                }
                _context.SaveChanges();
            }

            if (!_context.Sys_Strategorys.Any())
            {
                var strategory =
                    new StrategoryModel
                    {
                        Code = "Founder",
                        Name = "创始人策略",
                        SortCode = 0,
                        IsSystem = true,
                        IsSuper = true,
                        Description = "创始人拥有App完整的操作权限。"
                    };

                _modelHandler.Create(strategory);
                _context.Add(strategory);
                _context.SaveChanges();
            }

            var founderStrategory = _context.Sys_Strategorys.First(x => x.Code == "Founder");

            if (!_context.Sys_StrategoryRoles.Any())
            {
                var model = new StrategoryRoleModel
                {
                    Id = System.Guid.NewGuid().ToString(),
                    RoleId = roleFounder.Id,
                    StrategoryId = founderStrategory.Id
                };

                _context.Add(model);
                _context.SaveChanges();
            }

            if (!_context.Sys_ModuleButtons.Any())
            {
                var moduleButtons = new ModuleButtonModel[]
                {
                    new ModuleButtonModel
                    {
                        Id=Guid.NewGuid().ToString(),
                        Code="SystemManage_ConfigManage_AppConfigForm",
                         Name="系统参数设置",
                         Icon="adb",
                         JsAction="btn_appconfig()",
                         ModuleButtonOpenType=ModuleButtonOpenType.Pop,
                         ModuleButtonPosition=ModuleButtonPosition.Initial,
                         ModuleId=_context.Sys_Modules.First(x=>x.Code=="SystemManage_ConfigManage").Id,
                         SortCode=100,
                         Url="/SystemManage/ConfigManage/AppConfigForm",
                         ModuleButtonShowType=ModuleButtonShowType.TextAndIcon,
                         ModuleButtonColorType=ModuleButtonColorType.ColorTheme,
                         IsSystem=true,
                         Layer=0
                    },

                    new ModuleButtonModel
                    {
                        Id=Guid.NewGuid().ToString(),
                        Code="SystemManage_Module_Create",
                         Name="新建",
                         Icon="add",
                         JsAction="btn_create()",
                         ModuleButtonOpenType=ModuleButtonOpenType.Pop,
                         ModuleButtonPosition=ModuleButtonPosition.Initial,
                         ModuleId=_context.Sys_Modules.First(x=>x.Code=="SystemManage_Module").Id,
                         SortCode=100,
                         Url="/SystemManage/Module/Create",
                         ModuleButtonShowType=ModuleButtonShowType.TextAndIcon,
                         ModuleButtonColorType=ModuleButtonColorType.ColorTheme,
                         IsSystem=true,
                         Layer=0
                    },
                    new ModuleButtonModel
                    {
                        Id=Guid.NewGuid().ToString(),
                        Code="SystemManage_Module_Edit",
                         Name="修改",
                         Icon="edit",
                         JsAction="btn_edit()",
                         ModuleButtonOpenType=ModuleButtonOpenType.Pop,
                         ModuleButtonPosition=ModuleButtonPosition.Selected,
                         ModuleId=_context.Sys_Modules.First(x=>x.Code=="SystemManage_Module").Id,
                         SortCode=200,
                         Url="/SystemManage/Module/Edit",
                         ModuleButtonShowType=ModuleButtonShowType.TextAndIcon,
                         ModuleButtonColorType=ModuleButtonColorType.ColorTheme,
                         IsSystem=true,
                         Layer=0
                    },
                    new ModuleButtonModel
                    {
                        Id=Guid.NewGuid().ToString(),
                        Code="SystemManage_Module_Details",
                         Name="查看",
                         Icon="remove_red_eye",
                         JsAction="btn_details()",
                         ModuleButtonOpenType=ModuleButtonOpenType.Pop,
                         ModuleButtonPosition=ModuleButtonPosition.Selected,
                         ModuleId=_context.Sys_Modules.First(x=>x.Code=="SystemManage_Module").Id,
                         SortCode=300,
                         Url="/SystemManage/Module/Details",
                         ModuleButtonShowType=ModuleButtonShowType.TextAndIcon,
                         ModuleButtonColorType=ModuleButtonColorType.ColorTheme,
                         IsSystem=true,
                         Layer=0
                    },
                    new ModuleButtonModel
                    {
                        Id=Guid.NewGuid().ToString(),
                        Code="SystemManage_Module_Delete",
                         Name="删除",
                         Icon="remove_circle_outline",
                         JsAction="btn_delete()",
                         ModuleButtonOpenType=ModuleButtonOpenType.Pop,
                         ModuleButtonPosition=ModuleButtonPosition.Selected,
                         ModuleId=_context.Sys_Modules.First(x=>x.Code=="SystemManage_Module").Id,
                         SortCode=400,
                         Url="/SystemManage/Module/Delete",
                         ModuleButtonShowType=ModuleButtonShowType.TextAndIcon,
                         ModuleButtonColorType=ModuleButtonColorType.ColorDanger,
                         IsSystem=true,
                         Layer=0
                    },
                    new ModuleButtonModel
                    {
                        Id=Guid.NewGuid().ToString(),
                        Code="SystemManage_ModuleClaims",
                         Name="声明",
                         Icon="lightbulb_outline",
                         JsAction="btn_claims()",
                         ModuleButtonOpenType=ModuleButtonOpenType.Pop,
                         ModuleButtonPosition=ModuleButtonPosition.Selected,
                         ModuleId=_context.Sys_Modules.First(x=>x.Code=="SystemManage_Module").Id,
                         SortCode=450,
                         Url="/SystemManage/ModuleClaims",
                         ModuleButtonShowType=ModuleButtonShowType.TextAndIcon,
                         ModuleButtonColorType=ModuleButtonColorType.ColorTheme,
                         IsSystem=true,
                         Layer=0
                    },
                    new ModuleButtonModel
                    {
                        Id=Guid.NewGuid().ToString(),
                        Code="SystemManage_ModuleButton",
                         Name="按钮管理",
                         Icon="touch_app",
                         JsAction="btn_buttons()",
                         ModuleButtonOpenType=ModuleButtonOpenType.Pop,
                         ModuleButtonPosition=ModuleButtonPosition.Selected,
                         ModuleId=_context.Sys_Modules.First(x=>x.Code=="SystemManage_Module").Id,
                         SortCode=500,
                         Url="/SystemManage/ModuleButton",
                         ModuleButtonShowType=ModuleButtonShowType.TextAndIcon,
                         ModuleButtonColorType=ModuleButtonColorType.ColorTheme,
                         IsSystem=true,
                         Layer=0
                    },
                    new ModuleButtonModel
                    {
                        Id=Guid.NewGuid().ToString(),
                        Code="SystemManage_Module_InitialButtons",
                         Name="按钮初始化",
                         Icon="autorenew",
                         JsAction="btn_autorenew()",
                         ModuleButtonOpenType=ModuleButtonOpenType.Pop,
                         ModuleButtonPosition=ModuleButtonPosition.Selected,
                         ModuleId=_context.Sys_Modules.First(x=>x.Code=="SystemManage_Module").Id,
                         SortCode=600,
                         Url="/SystemManage/ModuleButton/InitialButtons",
                         ModuleButtonShowType=ModuleButtonShowType.TextAndIcon,
                         ModuleButtonColorType=ModuleButtonColorType.ColorTheme,
                         IsSystem=true,
                         Layer=0
                    },
                    new ModuleButtonModel
                    {
                        Id=Guid.NewGuid().ToString(),
                        Code="SystemManage_Module_UpdateIsSystem",
                         Name="提级",
                         Icon="arrow_upward",
                         JsAction="btn_updateissystem()",
                         ModuleButtonOpenType=ModuleButtonOpenType.Pop,
                         ModuleButtonPosition=ModuleButtonPosition.Selected,
                         ModuleId=_context.Sys_Modules.First(x=>x.Code=="SystemManage_Module").Id,
                         SortCode=700,
                         Url="/SystemManage/Module/UpdateIsSystem",
                         ModuleButtonShowType=ModuleButtonShowType.TextAndIcon,
                         ModuleButtonColorType=ModuleButtonColorType.ColorTheme,
                         IsSystem=true,
                         Layer=0
                    },
                    new ModuleButtonModel
                    {
                        Id=Guid.NewGuid().ToString(),
                        Code="SystemManage_Module_UpdateIsNotSystem",
                         Name="降级",
                         Icon="arrow_downward",
                         JsAction="btn_updateisnotsystem()",
                         ModuleButtonOpenType=ModuleButtonOpenType.Pop,
                         ModuleButtonPosition=ModuleButtonPosition.Selected,
                         ModuleId=_context.Sys_Modules.First(x=>x.Code=="SystemManage_Module").Id,
                         SortCode=800,
                         Url="/SystemManage/Module/UpdateIsNotSystem",
                         ModuleButtonShowType=ModuleButtonShowType.TextAndIcon,
                         ModuleButtonColorType=ModuleButtonColorType.ColorTheme,
                         IsSystem=true,
                         Layer=0
                    }

                };
                _context.AddRange(moduleButtons);
                _context.SaveChanges();

            }

            var buttonParent = _context.Sys_ModuleButtons.AsNoTracking().FirstOrDefault(x => x.Code == "SystemManage_ModuleButton");
            if (buttonParent != null)
            {
                if (!_context.Sys_ModuleButtons.AsNoTracking().Any(x => x.ParentId == buttonParent.Id))
                {

                    var buttonButtons = new ModuleButtonModel[]
                    {

                            new ModuleButtonModel
                            {
                                Id=Guid.NewGuid().ToString(),
                                Code = "SystemManage_ModuleButton_Create",
                                Name = "新建",
                                Icon = "add",
                                JsAction = "btn_create()",
                                ModuleButtonOpenType = ModuleButtonOpenType.Pop,
                                ModuleButtonPosition = ModuleButtonPosition.Initial,
                                ModuleId = _context.Sys_Modules.First(x => x.Code == "SystemManage_Module").Id,
                                SortCode = 100,
                                ParentId = _context.Sys_ModuleButtons.First(x => x.Code == "SystemManage_ModuleButton").Id,
                                Url = "/SystemManage/ModuleButton/Create",
                                ModuleButtonShowType = ModuleButtonShowType.TextAndIcon,
                                ModuleButtonColorType = ModuleButtonColorType.ColorTheme,
                                IsSystem = true,
                                Layer = 1
                            },
                            new ModuleButtonModel
                            {
                                Id=Guid.NewGuid().ToString(),
                                Code = "SystemManage_ModuleButton_Edit",
                                Name = "修改",
                                Icon = "edit",
                                JsAction = "btn_edit()",
                                ModuleButtonOpenType = ModuleButtonOpenType.Pop,
                                ModuleButtonPosition = ModuleButtonPosition.Selected,
                                ModuleId = _context.Sys_Modules.First(x => x.Code == "SystemManage_Module").Id,
                                SortCode = 200,
                                ParentId = _context.Sys_ModuleButtons.First(x => x.Code == "SystemManage_ModuleButton").Id,
                                Url = "/SystemManage/ModuleButton/Edit",
                                ModuleButtonShowType = ModuleButtonShowType.TextAndIcon,
                                ModuleButtonColorType = ModuleButtonColorType.ColorTheme,
                                IsSystem = true,
                                Layer = 1
                            },
                            new ModuleButtonModel
                            {
                                Id=Guid.NewGuid().ToString(),
                                Code = "SystemManage_ModuleButton_Details",
                                Name = "查看",
                                Icon = "remove_red_eye",
                                JsAction = "btn_details()",
                                ModuleButtonOpenType = ModuleButtonOpenType.Pop,
                                ModuleButtonPosition = ModuleButtonPosition.Selected,
                                ModuleId = _context.Sys_Modules.First(x => x.Code == "SystemManage_Module").Id,
                                SortCode = 300,
                                ParentId = _context.Sys_ModuleButtons.First(x => x.Code == "SystemManage_ModuleButton").Id,
                                Url = "/SystemManage/ModuleButton/Details",
                                ModuleButtonShowType = ModuleButtonShowType.TextAndIcon,
                                ModuleButtonColorType = ModuleButtonColorType.ColorTheme,
                                IsSystem = true,
                                Layer = 1
                            },
                            new ModuleButtonModel
                            {
                                Id=Guid.NewGuid().ToString(),
                                Code = "SystemManage_ModuleButton_Delete",
                                Name = "删除",
                                Icon = "remove_circle_outline",
                                JsAction = "btn_delete()",
                                ModuleButtonOpenType = ModuleButtonOpenType.Pop,
                                ModuleButtonPosition = ModuleButtonPosition.Selected,
                                ModuleId = _context.Sys_Modules.First(x => x.Code == "SystemManage_Module").Id,
                                SortCode = 400,
                                ParentId = _context.Sys_ModuleButtons.First(x => x.Code == "SystemManage_ModuleButton").Id,
                                Url = "/SystemManage/ModuleButton/Delete",
                                ModuleButtonShowType = ModuleButtonShowType.TextAndIcon,
                                ModuleButtonColorType = ModuleButtonColorType.ColorDanger,
                                IsSystem = true,
                                Layer = 1
                            },
                            new ModuleButtonModel
                            {
                                Id=Guid.NewGuid().ToString(),
                                Code = "SystemManage_ModuleButtonClaims",
                                Name = "声明",
                                Icon = "lightbulb_outline",
                                JsAction = "btn_claims()",
                                ModuleButtonOpenType = ModuleButtonOpenType.Pop,
                                ModuleButtonPosition = ModuleButtonPosition.Selected,
                                ModuleId = _context.Sys_Modules.First(x => x.Code == "SystemManage_Module").Id,
                                SortCode = 450,
                                ParentId = _context.Sys_ModuleButtons.First(x => x.Code == "SystemManage_ModuleButton").Id,
                                Url = "/SystemManage/ModuleButtonClaims",
                                ModuleButtonShowType = ModuleButtonShowType.TextAndIcon,
                                ModuleButtonColorType = ModuleButtonColorType.ColorTheme,
                                IsSystem = true,
                                Layer = 1
                            },
                            new ModuleButtonModel
                            {
                                Id=Guid.NewGuid().ToString(),
                                Code = "SystemManage_ModuleButton_UpdateIsSystem",
                                Name = "提级",
                                Icon = "arrow_upward",
                                JsAction = "btn_updateissystem()",
                                ModuleButtonOpenType = ModuleButtonOpenType.Pop,
                                ModuleButtonPosition = ModuleButtonPosition.Selected,
                                ModuleId = _context.Sys_Modules.First(x => x.Code == "SystemManage_Module").Id,
                                SortCode = 500,
                                ParentId = _context.Sys_ModuleButtons.First(x => x.Code == "SystemManage_ModuleButton").Id,
                                Url = "/SystemManage/ModuleButton/UpdateIsSystem",
                                ModuleButtonShowType = ModuleButtonShowType.TextAndIcon,
                                ModuleButtonColorType = ModuleButtonColorType.ColorTheme,
                                IsSystem = true,
                                Layer = 1
                            },
                            new ModuleButtonModel
                            {
                                Id=Guid.NewGuid().ToString(),
                                Code = "SystemManage_ModuleButton_UpdateIsNotSystem",
                                Name = "降级",
                                Icon = "arrow_downward",
                                JsAction = "btn_updateisnotsystem()",
                                ModuleButtonOpenType = ModuleButtonOpenType.Pop,
                                ModuleButtonPosition = ModuleButtonPosition.Selected,
                                ModuleId = _context.Sys_Modules.First(x => x.Code == "SystemManage_Module").Id,
                                SortCode = 600,
                                ParentId = _context.Sys_ModuleButtons.First(x => x.Code == "SystemManage_ModuleButton").Id,
                                Url = "/SystemManage/ModuleButton/UpdateIsNotSystem",
                                ModuleButtonShowType = ModuleButtonShowType.TextAndIcon,
                                ModuleButtonColorType = ModuleButtonColorType.ColorTheme,
                                IsSystem = true,
                                Layer = 1
                            }
                    };
                    _context.AddRange(buttonButtons);
                    _context.SaveChanges();
                }

            }
        }
    }
}
